import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useGameStore = defineStore('game', () => {
  // 当前游戏状态
  const currentRoom = ref(null)
  const gameMode = ref('classic') // classic, trpg
  const isKP = ref(false)
  
  // 角色卡数据
  const character = ref({
    name: '',
    stats: {
      str: 50, // 力量
      dex: 50, // 敏捷
      pow: 50, // 意志
      con: 50, // 体质
      app: 50, // 外貌
      siz: 50, // 体型
      int: 50, // 智力
      edu: 50, // 教育
    },
    skills: {},
    inventory: [],
    san: 99, // 理智值
    hp: 0, // 生命值
    mp: 0, // 魔法值
  })

  // 骰子历史
  const diceHistory = ref([])

  // 地图数据
  const mapData = ref({
    fog: [], // 迷雾区域
    rooms: [], // 房间
    npcs: [], // NPC
    items: [], // 物品
    triggers: [] // 触发器
  })

  // 计算生命值
  const calculateHP = () => {
    return Math.floor((character.value.stats.con + character.value.stats.siz) / 2)
  }

  // 计算魔法值
  const calculateMP = () => {
    return character.value.stats.pow
  }

  // 更新角色属性
  const updateCharacter = (updates) => {
    Object.assign(character.value, updates)
    character.value.hp = calculateHP()
    character.value.mp = calculateMP()
  }

  // 添加骰子记录
  const addDiceRecord = (record) => {
    diceHistory.value.unshift(record)
    if (diceHistory.value.length > 100) {
      diceHistory.value = diceHistory.value.slice(0, 100)
    }
  }

  // 加入房间
  const joinRoom = (roomData) => {
    currentRoom.value = roomData
  }

  // 离开房间
  const leaveRoom = () => {
    currentRoom.value = null
    isKP.value = false
  }

  return {
    currentRoom,
    gameMode,
    isKP,
    character,
    diceHistory,
    mapData,
    updateCharacter,
    addDiceRecord,
    joinRoom,
    leaveRoom
  }
})
